package com.example.zhangwei.ui.activityfragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.example.zhangwei.R;
import com.example.zhangwei.ui.activityfragment.fragment.OneFragment;
import com.example.zhangwei.ui.activityfragment.fragment.ThreeFragment;
import com.example.zhangwei.ui.activityfragment.fragment.TwoFragment;

public class MyFragmentActivity extends FragmentActivity{
	
	private TextView lastView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_index_fragment);
		
		
		lastView = (TextView)findViewById(R.id.radio_one);
		lastView.setBackgroundResource(R.drawable.radio_line_bg);
		Fragment details = (Fragment) getSupportFragmentManager().findFragmentByTag(R.id.radio_one+"");
		details = new OneFragment(MyFragmentActivity.this);
		setBody(details,R.id.radio_one, false);
	}
	
	
	public void clickBottom(View view){
    	int checkedId = view.getId();
    	if (lastView != null)
    		lastView.setBackgroundResource(R.drawable.transparent);
    	view.setBackgroundResource(R.drawable.radio_line_bg);
    	lastView = (TextView) view;
		Fragment details = (Fragment) getSupportFragmentManager().findFragmentByTag(checkedId+"");
		switch (checkedId) {
		case R.id.radio_one:
			if(details==null){
				details = new OneFragment(MyFragmentActivity.this);
				
			}		
			break;
		case R.id.radio_two:
			if(details==null){
				details = new TwoFragment(MyFragmentActivity.this);
				setBody(details,checkedId, false);
			}
			
			break;
		case R.id.radio_three:
		case R.id.radio_four:
			if(details==null){
				details = new ThreeFragment();
				setBody(details,checkedId, false);
			}		
			break;
		}
		setBody(details,checkedId, false);
		
    }
	
	public void setBody(Fragment bf,int id,boolean isback){
		if(bf==null){
			return;
		}
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
		transaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
	    transaction.replace(R.id.body, bf, id+"");
	    if (isback)
	    	transaction.addToBackStack(null);
	    transaction.commit();
	}

}
